/**
 * Created by Administrator on 2018-09-19.
 * 航班今日动态
 */

define(function (require,exports,module) {
    var sTpl = require("./flightDynamicToday.html");
    require("./flightDynamicToday.css");
    require("../../lib/fullcalendar/moment.min.js");
    var DEFINE_STATUS = require("../util/status");

    var VueComponent = Vue.extend({
        template: sTpl,
        methods:{
            /*出港*/
            btnClass: function (processStatus,status) {
                if(status=='本站待飞'){
                    return  processStatus>=this.DEFINE_STATUS.statusNum.EXPECTED_TO_FLY?true:false;
                }
                if(status=='本站起飞'){
                    return processStatus>=this.DEFINE_STATUS.statusNum.TAKE_OFF?true:false;
                }
                if(status=='外站预计落地'){
                    return processStatus>=this.DEFINE_STATUS.statusNum.OUTSIDE_EXPECTED_TO_FALL?true:false;
                }
                if(status=='站外近空'){
                    return  processStatus>=this.DEFINE_STATUS.statusNum.OUTSIDE_NEAR_SPACE?true:false;
                }
                if(status=='站外着陆'){
                    return processStatus>=this.DEFINE_STATUS.statusNum.OUTSIDE_LANDED?true:false;
                }

            },
            /*进港*/
            btnClass1:function (processStatus,status) {
                if(status=='站外待飞'){
                    return  processStatus>=this.DEFINE_STATUS.statusNum.OUTSIDE_EXPECTED_TO_FLY?true:false;
                }
                if(status=='站外起飞'){
                    return processStatus>=this.DEFINE_STATUS.statusNum.OUTSIDE_TAKE_OFF?true:false;
                }
                if(status=='预计落地'){
                    return processStatus>=this.DEFINE_STATUS.statusNum.EXPECTED_TO_FALL?true:false;
                }
                if(status=='近空'){
                    return processStatus>=this.DEFINE_STATUS.statusNum.NEAR_SPACE?true:false;
                }
                if(status=='本站着陆'){
                    return processStatus>=this.DEFINE_STATUS.statusNum.LANDED?true:false;
                }
            },
            confirmDelay:function () {
                var $this = this;
                this.$refs.delayType.validate(function(validate){
                });
            },
            /* 到达近空*/
            nearSpace:function (scope,inOrOut) {
                $this=this;
                var string ="你确定"+scope.row.flightInfo.flightNum+"航班已到达近空领域?";
                var string1=scope.row.flightInfo.flightNum+"航班已到达近空领域!";
                this.$confirm(string,'是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    axios.post(basepath+"/api/flightPlane/arriveNearSpace?planeId="+scope.row.planeId+"&inOrOut="+inOrOut).then(function (response) {
                        if(inOrOut=='出港'){
                            $this.$set($this.tableData,scope.$index,response.data);
                        }else if(inOrOut=='进港'){
                            $this.$set($this.tableData1,scope.$index,response.data);
                        }
                        $this.$message({
                            type: 'success',
                            message: string1
                        });
                    }).catch(function() {

                    });

                }).catch(function(){
                    $this.$message({
                        type: 'info',
                        message: '已取消该操作！'
                    });
                });
            },
            /*飞机落地*/
            landing:function (scope,inOrOut) {
                $this=this;
                var string ="你确定"+scope.row.flightInfo.flightNum+"航班已落地?";
                var string1=scope.row.flightInfo.flightNum+"航班已落地!";
                this.$confirm(string,'是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    axios.post(basepath+"/api/flightPlane/landing?planeId="+scope.row.planeId+"&inOrOut="+inOrOut).then(function (response) {
                        if(inOrOut=='出港'){
                            $this.$set($this.tableData,scope.$index,response.data);
                        }else if(inOrOut=='进港'){
                            $this.$set($this.tableData1,scope.$index,response.data);
                        }
                        $this.$message({
                            type: 'success',
                            message: string1
                        });
                    }).catch(function(_) {

                    });

                }).catch(function(){
                    $this.$message({
                        type: 'info',
                        message: '已取消该操作！'
                    });
                });
            },
            /*准备起飞*/
            expectedToFly:function (scope,inOrOut) {
                $this=this;
                var string ="你确定"+scope.row.flightInfo.flightNum+"航班已准备起飞?";
                var string1=scope.row.flightInfo.flightNum+"航班已准备起飞!";
                this.$confirm(string,'是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    axios.post(basepath+"/api/flightPlane/expectedToFlylanding?planeId="+scope.row.planeId+"&inOrOut="+inOrOut).then(function (response) {
                        if(inOrOut=='出港'){
                            $this.$set($this.tableData,scope.$index,response.data);
                        }else if(inOrOut=='进港'){
                            $this.$set($this.tableData1,scope.$index,response.data);
                        }
                        $this.$message({
                            type: 'success',
                            message: string1
                        });
                    }).catch(function(_) {

                    });

                }).catch(function(){
                    $this.$message({
                        type: 'info',
                        message: '已取消该操作！'
                    });
                });
            },
            /*起飞*/
            takeOff:function (scope,inOrOut) {
                $this=this;
                var string ="你确定"+scope.row.flightInfo.flightNum+"航班已起飞?";
                var string1=scope.row.flightInfo.flightNum+"航班已起飞!";
                this.$confirm(string,'是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    axios.post(basepath+"/api/flightPlane/takeOff?planeId="+scope.row.planeId+"&inOrOut="+inOrOut).then(function (response) {
                        if(inOrOut=='出港'){
                            $this.$set($this.tableData,scope.$index,response.data);
                        }else if(inOrOut=='进港'){
                            $this.$set($this.tableData1,scope.$index,response.data);
                        }
                        $this.$message({
                            type: 'success',
                            message: string1
                        });
                    }).catch(function(_) {

                    });

                }).catch(function(){
                    $this.$message({
                        type: 'info',
                        message: '已取消该操作！'
                    });
                });
            },
            /*预计落地航班*/
            expectedToFall:function (scope,inOrOut) {
                $this=this;
                var string ="你确定"+scope.row.flightInfo.flightNum+"航班为预计落地航班?";
                var string1=scope.row.flightInfo.flightNum+"航班已确定为预计落地航班!";
                this.$confirm(string,'是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    axios.post(basepath+"/api/flightPlane/expectedToFall?planeId="+scope.row.planeId+"&inOrOut="+inOrOut).then(function (response) {
                        if(scope.row.importOrExport=='出港'){
                            $this.$set($this.tableData,scope.$index,response.data);
                        }else if(scope.row.importOrExport=='进港'){
                            $this.$set($this.tableData1,scope.$index,response.data);
                        }
                        $this.$message({
                            type: 'success',
                            message: string1
                        });
                    }).catch(function(_) {

                    });

                }).catch(function(){
                    $this.$message({
                        type: 'info',
                        message: '已取消该操作！'
                    });
                });
            },
            /*----------------------------------------------------------------------*/
            delay:function(scope){
                this.dialogVisibleFdt=true;
                this.delayReasonForm.delayReason='';
                this.delayInformationPrompt="你确定"+scope.row.flightInfo.flightNum+"航班为延误航班？";
                this.delayInformationPrompt1=scope.row.flightInfo.flightNum+"航班已确定为延误航班!";
                this.delayFlightPlaneId=scope.row.planeId;
                this.delayTableDataIndex=scope.$index;
                this.deimportOrExport=scope.row.importOrExport;
            },
            /*确定飞机延误*/
            confirmDelay:function () {
                $this=this;
                this.$refs.delayReasonForm.validate(function(validate){
                    if(validate){
                        axios.post(basepath+"/api/flightPlane/confirmDelay?planeId="+$this.delayFlightPlaneId+"&delayReason="+$this.delayReasonForm.delayReason).then(function (response) {
                            if($this.deimportOrExport=='出港'){
                                $this.$set($this.tableData,$this.delayTableDataIndex,response.data);
                            }else if($this.deimportOrExport=='进港'){
                                $this.$set($this.tableData1,$this.delayTableDataIndex,response.data);
                            }
                            $this.$notify({
                                title: '操作成功！',
                                message: $this.delayInformationPrompt1
                            });
                            $this.dialogVisibleFdt=false;
                        })
                    }else{
                        $this.$notify({
                            title: '操作失败！',
                            message: '操作失败！'
                        });
                        return  false;
                    }

                });
            },
            cancel:function () {
                $this.$message({
                    type: 'info',
                    message: '已取消该操作！'
                });
                this.dialogVisibleFdt=false;
            },
            /*---------------------------------------------------------------*/
            /*停机位分配*/
            gpAllocation:function (scope) {
                this.findGp();
                this.dialogVisibleGpa=true;
                this.gpaFlightPlaneId=scope.row.planeId;
                this.gpaTableData1Index=scope.$index;
                this.scope=scope;
                this.gpAllocationForm.gpId='';
            },
            cancelGpa:function () {
                $this.$message({
                    type: 'info',
                    message: '已取消该操作！'
                });
                this.dialogVisibleGpa=false;
            },
            confirmGpa:function () {
                this.dialogVisibleGpa=true;
                if(this.scope.row.processStatus==this.DEFINE_STATUS.statusNum.LANDED){
                    $this=this;
                    if($this.gp.runStatus!=$this.DEFINE_STATUS.statusNum.OCCUPY){
                        this.$refs.gpAllocationForm.validate(function(validate){
                            if(validate) {
                                axios.post(basepath + "/api/flightPlane/gpa?planeId=" + $this.gpaFlightPlaneId + "&gpId=" + $this.gpAllocationForm.gpId).then(function (response) {
                                    $this.$set($this.tableData1, $this.gpaTableData1Index, response.data);
                                    $this.$notify({
                                        title: '操作成功！',
                                        message: '操作成功！'
                                    });
                                    $this.findGp();
                                    $this.dialogVisibleGpa = false;
                                })
                            } else {
                                $this.$notify({
                                    title: '操作失败！',
                                    message: '操作失败！'
                                });
                                return false;
                            }
                        });
                    }

                    else {
                        $this.$notify({
                            title: '操作失败！',
                            message: '该机位已占用！'
                        });
                        return  false;
                    }
                }else{
                    this.$refs.gpAllocationForm.validate(function(validate){
                        if(validate) {
                            axios.post(basepath + "/api/flightPlane/gpa?planeId=" + $this.gpaFlightPlaneId + "&gpId=" + $this.gpAllocationForm.gpId).then(function (response) {
                                $this.$set($this.tableData1, $this.gpaTableData1Index, response.data);
                                $this.$notify({
                                    title: '操作成功！',
                                    message: '操作成功！'
                                });
                                $this.findGp();
                                $this.dialogVisibleGpa = false;
                            })
                        } else {
                            $this.$notify({
                                title: '操作失败！',
                                message: '操作失败！'
                            });
                            return false;
                        }
                    });
                }


            },
            /*获取未分配未占用的机位的数据*/
            findGp:function () {
                var $this = this;
                axios.get(basepath+"/api/sysSetting/findGp"
                ).then(function (response) {
                    $this.gpas=response.data;
                });
            },
            findGpById:function (a) {
                var $this = this;
                this.gp=this.gpas.find(function (value) {
                    return a==value.gpId
                })
            },
            /*--------------------------------------------------------*/
            cellDblclick:function (row, event) {
                this.visible1=true;
            },
            getCellClass:function (row) {
                if(row.column.label=="保障状态"){
                    return "center-cell";
                }
            },
            tableSort:function(){

            },

            zeroPadding:function(num, digit) {
                var zero = '';
                for(var i = 0; i < digit; i++) {
                    zero += '0';
                }
                return (zero + num).slice(-digit);
            },
            checkFlightDynamic:function () {
                var $this = this;
                var date = moment(new Date()).format("YYYY-MM-DD");
                var url;
                var url1;
                if(this.checkForm.flightDynamicDate){
                    url= this.baseUrl +"&importOrExport=出港&flightNum="+this.checkForm.flightNum+"&estimatedTime="+this.checkForm.flightDynamicDate;
                    url1=this.baseUrl +"&importOrExport=进港&flightNum="+this.checkForm.flightNum+"&estimatedTime="+this.checkForm.flightDynamicDate;
                }else{
                    url= this.baseUrl +"&importOrExport=出港&flightNum="+this.checkForm.flightNum+"&estimatedTime="+date;
                    url1=this.baseUrl +"&importOrExport=进港&flightNum="+this.checkForm.flightNum+"&estimatedTime="+date;
                }


                /*获取进港数据*/
                axios.get(url
                ).then(function (response) {
                    $this.tableData=response.data;
                });
                /*获取出港数据*/
                axios.get(url1
                ).then(function (response) {
                    $this.tableData1=response.data;
                });

            }

        },
        data:function(){
            return {
                scope:{},
                gpaTableData1Index:'',
                gpaFlightPlaneId:'',
                dialogVisibleGpa:false,
                DEFINE_STATUS:DEFINE_STATUS,
                deimportOrExport:'',
                delayTableDataIndex:'',
                delayFlightPlaneId:'',
                delayInformationPrompt:'',
                delayInformationPrompt1:'',
                delayReasonForm:{delayReason:''},
                gpAllocationForm:{
                    gpId:''
                },
                gp:'',
                gpas:[],
                delayReasons : [{
                    value: '天气',
                    label: '天气'
                }, {
                    value: '故障',
                    label: '故障'
                }, {
                    value: '军演',
                    label: '军演'
                }, {
                    value: '机场限制',
                    label: '机场限制'
                }, {
                    value: '其它',
                    label: '其它'
                }],
                dialogVisibleFdt:false,
                tableData:[],
                tableData1:[],
                importOrExport:'',
                checkForm:{
                    flightNum:"",
                    flightDynamicDate:''
                },
                baseUrl:basepath+"/api/flightPlane/findFlightDynamicPlaneToday?"
            }
        },
        created:function (){
            this.findGp();
            this.checkFlightDynamic();
            $this=this;
        },
        activated:function () {
            messages.$emit('change','今日动态');
        }

    });

    module.exports = VueComponent;
})
